package com.bjsxt.shop.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.user.dao.IAadminsDao;
import com.bjsxt.shop.user.dao.IRoleDao;
import com.bjsxt.shop.users.pojo.ARole;
import com.bjsxt.shop.users.pojo.Aadmins;
import com.bjsxt.shop.users.service.IUserService;

/**
 * 用户相关的Service实现类
 *
 */
@Service("userservice")
public class UserServiceImpl implements IUserService
{
	@Resource
	private IAadminsDao adminsDao;
	@Resource
	private IRoleDao roleDao;

	@Override
	public JSONObject saveOneAadminsService(Aadmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.save(admins);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "保存成功");
			// 存储data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "保存失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAadminsService(Aadmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.update(admins);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			JSONObject dataJSON = new JSONObject();
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}

		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAadminsService(Aadmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.delet(admins);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			JSONObject dataJSON = new JSONObject();
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}

		return resultJSON;
	}

	@Override
	public Aadmins findOneAadminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<Aadmins> findCondListAadminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{ /* 为关键字加上%,实现模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil != null)
		{
			// 分页
			condMap.put("pageCount", "true");
			List <Aadmins> adminsList = this.adminsDao.findCondList(condMap);
			/* 取总记录数 */
			if (adminsList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			}

			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.adminsDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveOneARoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.save(role);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "保存成功");
			// 存储data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "保存失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.update(role);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			JSONObject dataJSON = new JSONObject();
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}

		return resultJSON;
	}

	@Override
	public JSONObject deleteOneARoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.delet(role);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			JSONObject dataJSON = new JSONObject();
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}

		return resultJSON;
	}

	@Override
	public ARole findOneARoleService(Map<String, Object> condMap)
	{
		return this.roleDao.findOne(condMap);
	}

	@Override
	public List<ARole> findCondListARoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil != null)
		{
			// 分页
			condMap.put("pageCount", true);
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			/* 取总记录数 */
			if (roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}

			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.roleDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.roleDao.findCondList(condMap);
	}

}
